{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     sex  age  smoke  height\n0    man   15   True     168\n1    man   23  False     179\n2  women   25  False     181\n3  women   17   True     166\n4    man   35   True     173\n5  women   57  False     178\n6    man   24  False     188\n7  women   31   True     190\n8  women   22  False     160",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sex</th>\n      <th>age</th>\n      <th>smoke</th>\n      <th>height</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>man</td>\n      <td>15</td>\n      <td>True</td>\n      <td>168</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>man</td>\n      <td>23</td>\n      <td>False</td>\n      <td>179</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>women</td>\n      <td>25</td>\n      <td>False</td>\n      <td>181</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>women</td>\n      <td>17</td>\n      <td>True</td>\n      <td>166</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>man</td>\n      <td>35</td>\n      <td>True</td>\n      <td>173</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>women</td>\n      <td>57</td>\n      <td>False</td>\n      <td>178</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>man</td>\n      <td>24</td>\n      <td>False</td>\n      <td>188</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>women</td>\n      <td>31</td>\n      <td>True</td>\n      <td>190</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>women</td>\n      <td>22</td>\n      <td>False</td>\n      <td>160</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = {'sex':['man','man','women','women','man','women','man','women','women'],\n",
    "'age':[15,23,25,17,35,57,24,31,22],\n",
    "'smoke':[True,False,False,True,True,False,False,True,False],\n",
    "'height':[168,179,181,166,173,178,188,190,160]}\n",
    "df = pd.DataFrame(data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "                   age  height\nsex   smoke                   \nman   False  23.500000   183.5\n      True   25.000000   170.5\nwomen False  34.666667   173.0\n      True   24.000000   178.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>age</th>\n      <th>height</th>\n    </tr>\n    <tr>\n      <th>sex</th>\n      <th>smoke</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">man</th>\n      <th>False</th>\n      <td>23.500000</td>\n      <td>183.5</td>\n    </tr>\n    <tr>\n      <th>True</th>\n      <td>25.000000</td>\n      <td>170.5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">women</th>\n      <th>False</th>\n      <td>34.666667</td>\n      <td>173.0</td>\n    </tr>\n    <tr>\n      <th>True</th>\n      <td>24.000000</td>\n      <td>178.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "#以性别为分组依据，查看男女抽烟平均年龄和身高\n",
    "pd.pivot_table(df,index=['sex','smoke'],values=['age','height'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "smoke  False  True \nsex                \nman        2      2\nwomen      3      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>smoke</th>\n      <th>False</th>\n      <th>True</th>\n    </tr>\n    <tr>\n      <th>sex</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>man</th>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>women</th>\n      <td>3</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "#统计各个性别抽烟的人数\n",
    "pd.crosstab(df.sex,df.smoke)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "smoke  False  True \nage                \n15         0      1\n17         0      1\n22         1      0\n23         1      0\n24         1      0\n25         1      0\n31         0      1\n35         0      1\n57         1      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>smoke</th>\n      <th>False</th>\n      <th>True</th>\n    </tr>\n    <tr>\n      <th>age</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>15</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>22</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>24</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>25</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>31</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>35</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>57</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "#统计各个年龄段抽烟人情况\n",
    "pd.crosstab(df.age,df.smoke)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}